package com.dinner.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dinner.auto.JSONAuthentication;
import com.dinner.entity.DiMenu;
import com.dinner.entity.DiUser;
import com.dinner.mapper.DiMenuMapper;
import com.dinner.mapper.DiUserMapper;
import com.dinner.response.Result;
import com.dinner.service.DiMenuService;
import com.dinner.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dinner
 * @since 2022-02-12
 */
@RestController
@RequestMapping("/di-menu")
public class DiMenuController extends JSONAuthentication {
    @Autowired
    DiMenuMapper diMenuMapper;
    @Autowired
    DiMenuService diMenuService;
    @ApiOperation(value = "添加菜品")
    @PostMapping("/add")
    public void addMenu(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response,@RequestParam("meName") String meName,@RequestParam("meDescribe") String meDescribe,@RequestParam("mePrice") float mePrice) throws Exception{
        DiMenu diMenu=new DiMenu();
        diMenu.setMeName(meName);
        diMenu.setMeDescribe(meDescribe);
        diMenu.setMePrice(mePrice);

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("me_name",diMenu.getMeName());
        if(diMenuMapper.selectCount(wrapper)!=0){
            Result result=Result.error().message("该菜品名已存在");
            this.WriteJSON(request, response, result);
        }else{
            byte[] bytes = file.getBytes();
            String imageFileName = file.getOriginalFilename();
            String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
            Path path = Paths.get("D:\\自己的项目\\订餐系统\\order\\src\\assets\\picture\\" + fileName);
            Files.write(path, bytes);//写入文件
            String avatar_url=fileName;
            diMenu.setMePicture(avatar_url);
            response.reset();
            diMenuMapper.insert(diMenu);
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }

    }
    @ApiOperation(value = "删除菜品")
    @PostMapping("/del")
    public void delMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=diMenuMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改菜品有无货")
    @PostMapping("/updMenuStatus")
    public void updMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
      DiMenu diMenu=diMenuMapper.selectById(id);
      if(diMenu.getMeStatus()>0){
          diMenu.setMeStatus(0);
          int i=diMenuMapper.updateById(diMenu);
          if(i>0){
              Result ok = Result.ok();
              this.WriteJSON(request, response, ok);
          } else {
              Result error = Result.error();
              this.WriteJSON(request, response, error);
          }
      }else{
          diMenu.setMeStatus(1);
          int i=diMenuMapper.updateById(diMenu);
          if(i>0){
              Result ok = Result.ok();
              this.WriteJSON(request, response, ok);
          } else {
              Result error = Result.error();
              this.WriteJSON(request, response, error);
          }
      }

    }
    @ApiOperation(value = "修改菜品价格")
    @PostMapping("/updMenuPrice")
    public void updMenua(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ,@RequestParam("mePrice") float mePrice) throws Exception{
        DiMenu diMenu=diMenuMapper.selectById(id);
        diMenu.setMePrice(mePrice);
        int i=diMenuMapper.updateById(diMenu);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看菜品")
    @PostMapping("/lookMenu")
    public void lookMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size ,@RequestParam("current") int current) throws Exception{
        Page<DiMenu> page=new Page<>(current,size);
        Page<DiMenu> pages=diMenuMapper.selectPage(page,null);
        List<DiMenu> list=pages.getRecords();
        int total=diMenuMapper.selectCount(null);
        if(list.size()>0){
            Result ok = Result.ok().data("menu",list).data("total",total);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("暂无菜品，去叫商家添加一个吧");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "模糊查看菜品通过菜名")
    @PostMapping("/lookMenuByName")
    public void lookMenuByName(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size ,@RequestParam("current") int current,@RequestParam("meName") String meName) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("me_name",meName);
        Page<DiMenu> page=new Page<>(current,size);
        Page<DiMenu> pages=diMenuMapper.selectPage(page,wrapper);
        List<DiMenu> list=pages.getRecords();
        int total=diMenuMapper.selectCount(wrapper);
        if(list.size()>0){
            Result ok = Result.ok().data("menu",list);
            ok.data("total",total);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("暂无菜品，去叫商家添加一个吧");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看菜品通过菜品id")
    @PostMapping("/lookMenuById")
    public void lookMenuByid(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
       if(diMenuMapper.selectById(id)!=null){
           Result ok = Result.ok().data("menu",diMenuMapper.selectById(id));
           this.WriteJSON(request, response, ok);
       }else{
           Result error = Result.error().message("没查询出来");
           this.WriteJSON(request, response, error);
       }

    }
    @ApiOperation(value = "降序查看菜品销量")
    @PostMapping("/lookMenuDesc")
    public void lookMenuDesc(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size,@RequestParam("current") int current ) throws Exception{
       List<DiMenu> list=diMenuService.findMenuDesc(current,size);
       int total=diMenuMapper.selectCount(null);
        if(list.size()>1){
            Result ok = Result.ok().data("menu",list).data("total",total);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("暂无菜品");
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "查看同类型菜品再通过销量排序")
    @PostMapping("/lookMenuByIdByType")
    public void lookMenuByidaa(HttpServletRequest request, HttpServletResponse response, @RequestParam("menuType") String  menuType,@RequestParam("menuId") int menuid ) throws Exception{
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("menu_type",menuType);
        wrapper.orderByDesc("menu_sale");
        wrapper.ne("id",menuid);
        wrapper.last("limit 3");

        List<DiMenu> diMenus=diMenuMapper.selectList(wrapper);

        if(diMenus!=null){
            Result ok = Result.ok().data("menu",diMenus);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("没查询出来");
            this.WriteJSON(request, response, error);
        }
    }
}

